package com.salton123.charging.util;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.MediaStore.Images.Media;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class IOUtils {

    public static String getSoundUriBySoundIdKey(String str) {
        return DBUtils.getNameById(PrefsUtils.get().getInt(str, 1));
    }

    public static File getCacheFile(Context context, String str) {
        return new File(context.getExternalFilesDir(null), str);
    }

    public static void saveImageToGallery(Context context, Bitmap bitmap, File file) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            bitmap.compress(CompressFormat.JPEG, 60, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
            Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null);
            context.sendBroadcast(new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE", Uri.fromFile(file)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void enableAllSSLHandshake() {
        try {
            TrustManager[] trustManagerArr = new TrustManager[]{new X509TrustManager() {
                public void checkClientTrusted(X509Certificate[] x509CertificateArr, String str) {
                }

                public void checkServerTrusted(X509Certificate[] x509CertificateArr, String str) {
                }

                public X509Certificate[] getAcceptedIssuers() {
                    return new X509Certificate[0];
                }
            }};
            SSLContext instance = SSLContext.getInstance("TLS");
            instance.init(null, trustManagerArr, new SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(instance.getSocketFactory());
            // HttpsURLConnection.setDefaultHostnameVerifier($$Lambda$IOUtils$3Ore9x8XSAaeqGQaL_MwNDXAxM.INSTANCE);
        } catch (Exception unused) {
        }
    }

    public static String getExceptionStackInfo(Throwable th) {
        StringWriter stringWriter = new StringWriter();
        th.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

    public static Uri convertResIdToUri(Context context, int i) {
        Resources resources = context.getResources();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("android.resource://");
        stringBuilder.append(resources.getResourcePackageName(i));
        String str = "/";
        stringBuilder.append(str);
        stringBuilder.append(resources.getResourceTypeName(i));
        stringBuilder.append(str);
        stringBuilder.append(resources.getResourceEntryName(i));
        return Uri.parse(stringBuilder.toString());
    }
}
